package cn.ghf.org.service.impl;

import cn.ghf.basic.exception.MyException;
import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.org.domain.Employee;
import cn.ghf.org.domain.Shop;
import cn.ghf.org.mapper.EmployeeMapper;
import cn.ghf.org.mapper.ShopMapper;
import cn.ghf.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService{

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Override
    @Transactional
    public void setTlement(Shop shop) {
        //获取管理员
        Employee admin = shop.getAdmin();
        //判断用户是否存在，用户名，电话，邮箱都可以当用户名
        Employee employee=employeeMapper.loadByUsername(admin);
        //用户已存在
        if(employee != null){
            throw new MyException("用户或邮箱或电话已存在");
        }
        //用户不存在，将admin信息存入,主键自增
        employeeMapper.add(admin);
        //将admin.id 存入shop里,主键自增，获取主键
        shopMapper.add(shop);
        //将shop.id存到admin中
        admin.setShop(shop);
        employeeMapper.update(admin);

    }
}
